/*
 * 权限验证
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
*/
layui.extend({
  setter: 'config' //配置模块
}).define(['setter'],function(exports){ //提示：模块也可以依赖其它模块，

var setter = layui.setter;
var $ = layui.jquery;

var roleper={
	/*
     * 获取权限数据
     * 参数解释：
       url   
    */
    render:function(options){
       
        var that=this;
    	var data={};
        var url = options.url ? options.url : that.getmodular()+setter.roleurl;
        var per = options.per ? options.per : ''; 
        var mid = options.mid ? options.mid : that.getpathname(); 
        var law = options.law ? options.law : '';
        var full= options.full ? options.full : false;
        var par= options.par ? options.par : '';  //父窗口打开
        var refresh= options.refresh ? options.refresh : false;  //打开是否刷新
        var offset= options.offset ? options.offset : '100px'; //离顶部高度


        data = that.getdata(url,mid);
        checkrole = that.checkrole(data,per,law,full,par,refresh);
        return checkrole;
    	
    },
    showtool:function(options){

        var that=this;
        var url = options.url ? options.url : that.getmodular()+setter.roleurl;
        var mid = options.mid ? options.mid : that.getpathname();        
        data = that.getdata(url,mid);
        //兼容之前版本 20220511
        if(data.add || data.edit){
            return true;
        }
        // 20220511 新更新
        var per = options.per ? options.per : [{sys:'add'}];
        var show=[];        
        data = that.getdata(url,mid);
        (eval(per)).forEach((item) => {
            if(data[item.sys]){
               show.push(data[item.sys]);
            }
        })
        if(show.length>0){
           return true; 
        }else{
            return ''; 
        }
        
    },
    getdata:function(dataurl,data){
    	var roles={};
        $.ajax({
            url: dataurl,
            async:false,
            data:{'id':data},
            type: 'post',
            dataType: "json",
            success:function(R){
                if (R.code == 200) {
                    roles = R.data;
                } else {
                    roles = {};
                }
            },
            error: function (R) {
              // alert('failure to send to database');
            }
        });
        return roles
    },
    getpathname:function(){
        var pathname = window.location.pathname;
        var search = window.location.search;
        if(search){
           pathname = pathname + search;
        }
        return pathname;
    },
    getmodular:function(){
        var pathname = window.location.pathname;
        pathname=pathname.split("/"); //路径放到数组中
        return '/'+pathname[1];
    },

    getparam:function(data,key) {
   	    if((data instanceof Object)){
            return data; //数据为对象直接返回
   	    }
   	    // 使用typeof检查数据类型
   	    data=isNaN(Number(data))?data:Number(data);
   	    if (typeof data === 'string') {
   	    	data = data.replace(/'/g, '"'); 
       	    try {
           	    data = JSON.parse(data);// data转为对象或数组
           	    return data;
       	    } catch (e) {
       	    	var obj ={};
           	    obj[key]=data;
           	    return obj;
       	    }
   	    } else if (typeof data === 'number') {
       	    var obj ={};
           	obj[key]=data;
           	return obj;
   	    }
    },
    checkrole:function(data,per,law,fulls,par,refresh,offset){
    	var that=this;
        var width=law ? law.split('*')[0]+'px' : '1060px';
        var height=law ? law.split('*')[1]+'px' : '500px';
        $(document).ready(function() {
            (eval(per)).forEach((item) => {
                if(data[item.sys]){
                    $('.'+item.sys).show();
                    // $('.'+item.sys).html(data['name'][item.sys]); //通过此处可以直接显示后台设置的名称，后期可用，现不支持多国语言
                    $('.'+item.sys).click(function(e){
                        dataset=e.target.dataset.set;
                        var full=item.full==''? item.full:fulls;
                        if(item.law){
                            width=item.law.split('*')[0]+'px';
                            height=item.law.split('*')[1]+'px';
                        }
                        data['name'][item.sys]= data['name'][item.sys]? data['name'][item.sys]:'操作';
                        const key = data['param'][item.sys];
                        const obj =that.getparam(dataset,key);
                        switch(item.method){
                  	        case 'open':
                      	        const param = (key == '')?'':('?'+data['param'][item.sys]+'='+dataset);
                  	            xadmin.open({title:data['name'][item.sys],url:data['code']+data[item.sys]+param,width:width,height:height,full:full,par:par,refresh:refresh,offset:offset});
                  	            break;
                  	        case 'del':
                  	            xadmin.post({url:data['code']+data[item.sys],data:obj,msg:item.msg?item.msg:'',tip:item.tip?item.tip:'删除数据吗？会删除下属所有数据'});
                  	            break;
                  	        case 'send':
                  	            xadmin.post({url:data['code']+data[item.sys],data:obj,msg:item.msg?item.msg:'',tip:item.tip?item.tip:'确定发送数据吗？'});
                  	            break;
                  	        case 'post':
                  	            xadmin.post({url:data['code']+data[item.sys],data:obj,msg:item.msg?item.msg:'',tip:item.tip?item.tip:'确定提交数据吗？'});
                  	            break;
                            case 'get':
                  	            xadmin.get({url:data['code']+data[item.sys],data:obj,msg:item.msg?item.msg:'',tip:item.tip?item.tip:'确定获取数据吗？'});
                  	                break;
                  	        case 'self':
                  	            xadmin.self({url:data['code']+data[item.sys]+'?'+dataset})
                  	            break;
                  	        default:
                  	            break;
                        }
                    })
                }else{
                    $('.'+item.sys).remove();  
                }
            })
        })
    },
}
exports('roleper', roleper);
});
